﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace Presentation
{
    public partial class RPBangDiemMonhoc : Form
    {
        DataTable banglop, bangmon, hocky, bangreport;
        DataSet ForReport = new DataSet();
        int malop_chon = -1, hocky_chon = -1;
        string mamon_chon = null;
        public RPBangDiemMonhoc()
        {
            InitializeComponent();
        }
        private void LayDSLop()
        {
            banglop = BUSRPBangDiemMonHoc.LayDSLop();
            DataRow donggia = banglop.NewRow();
            donggia[0] = -1;
            donggia[1] = "Chọn Lớp";
            banglop.Rows.InsertAt(donggia, 0);
            comboBox_lop.DataSource = banglop;
            comboBox_lop.DisplayMember = "TenLop";
            comboBox_lop.ValueMember = "MaLop";
            comboBox_lop.SelectedIndex = 0;
            
        }
        private void LayDSMon(int ma)
        {
            bangmon = BUSRPBangDiemMonHoc.LayDSMon(ma);
            comboBox_monhoc.DataSource = bangmon;
            comboBox_monhoc.DisplayMember = "TenMonHoc";
            comboBox_monhoc.ValueMember = "MaMonHoc";
        }
        private void TaoHocKy()
        {
            hocky = new DataTable();
            DataColumn ten = new DataColumn("Học Kỳ", typeof(string));
            DataColumn giatri = new DataColumn("Mã HK", typeof(int));
            hocky.Columns.Add(ten);
            hocky.Columns.Add(giatri);
            DataRow giatri0 = hocky.NewRow();
            giatri0[0] = "Chọn Học Kỳ";
            giatri0[1] = -1;
            hocky.Rows.Add(giatri0);
            DataRow giatri1 = hocky.NewRow();
            giatri1[0] = "Học Kỳ I";
            giatri1[1] = 1;
            hocky.Rows.Add(giatri1);
            DataRow giatri2 = hocky.NewRow();
            giatri2[0] = "Học Kỳ II";
            giatri2[1] = 2;
            hocky.Rows.Add(giatri2);
        }
        private void RPBangDiemMonhoc_Load(object sender, EventArgs e)
        {
            TaoHocKy();
            comboBox_hocky.DataSource = hocky;
            comboBox_hocky.DisplayMember = "Học Kỳ";
            comboBox_hocky.ValueMember = "Mã HK";
            LayDSLop();
        }

        private void button_thoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void comboBox_lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
            int default_value;
            int.TryParse(comboBox_lop.SelectedValue.ToString(), out default_value);
            if (default_value != 0)
            {
                if (int.Parse(comboBox_lop.SelectedValue.ToString()) != -1)
                {
                    int ma = int.Parse(comboBox_lop.SelectedValue.ToString());
                    bangmon = BUSRPBangDiemMonHoc.LayDSMon(ma);                                        
                    DataRow donggia = bangmon.NewRow();
                    donggia[0] = -1;
                    donggia[1] = "Chọn Môn";
                    bangmon.Rows.InsertAt(donggia, 0);
                    comboBox_monhoc.DataSource = bangmon;
                    comboBox_monhoc.DisplayMember = "TenMonHoc";
                    comboBox_monhoc.ValueMember = "MaMonHoc";
                    comboBox_monhoc.SelectedIndex = 0;
                }
            }
           */
        }

        private void comboBox_monhoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void comboBox_hocky_SelectedIndexChanged(object sender, EventArgs e)
        {            
            if (comboBox_hocky.Focused)
            {
                int ma = int.Parse(comboBox_lop.SelectedValue.ToString());
                string hk = Convert.ToString(comboBox_hocky.SelectedValue);
                bangmon = BUSRPBangDiemMonHoc.LayDSMon(ma,hk);
                DataRow donggia = bangmon.NewRow();
                donggia[0] = -1;
                donggia[1] = "Chọn Môn";
                bangmon.Rows.InsertAt(donggia, 0);
                comboBox_monhoc.DataSource = bangmon;
                comboBox_monhoc.DisplayMember = "TenMonHoc";
                comboBox_monhoc.ValueMember = "MaMonHoc";
                comboBox_monhoc.SelectedIndex = 0;
            }            
        }

        private void button_thuchien_Click(object sender, EventArgs e)
        {
            if (comboBox_monhoc.SelectedIndex >= 0 && comboBox_lop.SelectedIndex >= 0 && comboBox_hocky.SelectedIndex >= 0)
            {
                mamon_chon = comboBox_monhoc.SelectedValue.ToString().Trim();
                malop_chon = int.Parse(comboBox_lop.SelectedValue.ToString().Trim());
                hocky_chon = int.Parse(comboBox_hocky.SelectedValue.ToString().Trim());
                if (malop_chon != -1 && mamon_chon != null && hocky_chon != -1)
                {
                    bangreport = BUSRPBangDiemMonHoc.KetQuaLoc(malop_chon, mamon_chon, hocky_chon);
                    DataColumn col = new DataColumn("STT");
                    bangreport.Columns.Add(col);
                    for (int i = 0; i < bangreport.Rows.Count; i++)
                    {
                        bangreport.Rows[i]["STT"] = i + 1;
                    }
                    dataGridView_ds.DataSource = bangreport;
                    dataGridView_ds.Columns["STT"].DisplayIndex = 0;
                    dataGridView_ds.Columns["STT"].Width = 48;
                    dataGridView_ds.Columns["HoTen"].HeaderText = "Họ Tên";
                    dataGridView_ds.Columns["HoTen"].Width = 150;
                    dataGridView_ds.Columns["Diem15p"].HeaderText = "Điểm 15 Phút";
                    dataGridView_ds.Columns["Diem15p"].Width = 130;
                    dataGridView_ds.Columns["Diem1tiet"].HeaderText = "Điểm 45 Phút";
                    dataGridView_ds.Columns["Diem1tiet"].Width = 130;
                    dataGridView_ds.Columns["DiemCuoiHocKy"].HeaderText = "Điểm Cuối Kỳ";
                    dataGridView_ds.Columns["DiemCuoiHocKy"].Width = 130;

                }
            }            
        }
        
    }
}
